<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

$msg = array();

//权限验证
if( Role::init() == false ){
	$msg['error'] = false;
	$msg['string'] = '权限不足！';
	$myjson = json_encode($msg); 
	echo $myjson; 
	exit;
}

$db = new DB('news');

$title_id = isset($_POST['title_id']) ? h($_POST['title_id']) : '';

$title_id_arr = explode('|',$title_id);
if(empty($title_id)){//title_id为空
	$msg['error'] = false;
	$msg['string'] = '参数有误！';
}else{
	$title_count = 0;
	$title_id_string = '';
	foreach($title_id_arr as $key=>$val){
		if( $val != '' ){
			$news_sql = 'select title_id,title,category_id,static_url,content from news where title_id="'.$val.'"';
			$news_rs = $db->query($news_sql)->getOne();
			if( $news_rs['category_id'] < 1 ){
				continue;
			}
			
			//更新is_del,edit_uid,updated字段
			$data = array('is_del'=>2,'edit_uid'=>Sessions::get('uid'),'updated'=>time());
			$db->table('news')->where('title_id="'.$val.'"')->update($data)->run();
			
			//更新counts字段
			$category_sql = 'select counts from categories where id='.$news_rs['category_id'];
			$category_rs = $db->query($category_sql)->getOne();
			$data=array('counts'=>($category_rs['counts']-1));
			$db->table('categories')->where('id='.$news_rs['category_id'])->update($data)->run();
			
			$title_count++;
			$title_id_string .= $val.',';
			
			//重命名静态文件
			/*$static_path = $root.'www'.substr($news_rs['static_url'], 20);
			$news_content_array = explode('_ueditor_page_break_tag_', $news_rs['content']);
			$pages = count($news_content_array);
			for( $page=0; $page<=$pages; $page++ ){
				if( $page == 0 ){
					$path = $static_path.'/';
				}else{
					$path = $static_path.'-'.$page.'/';
				}
				
				$old_php_filename = $path.'index.php';
				$new_php_filename = $path.'index_del.php';
				if( file_exists( $old_php_filename) ){
					@rename($old_php_filename, $new_php_filename);
				}
				
				$old_html_filename = $path.'index.html';
				$new_html_filename = $path.'index_del.html';
				if( file_exists( $old_html_filename) ){
					@rename($old_html_filename, $new_html_filename);
				}
			}*/
		}
	}
	$title_id_string = substr($title_id_string, 0, -1);
	
	//记录动作日志
	Log::logs_articles($news_rs['category_id'], 7, $title_id_string);

	$msg['error'] = true;
	$msg['string'] = '成功删除 '.$title_count.' 篇新闻!';
}

$myjson = json_encode($msg); 
echo $myjson; 
exit;